#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.db import models
from choices import *
import datetime

class User(models.Model):
    """Definition of User
    
    Necessary Fields are:
        email --> unique, for login
        name --> may NOT unique
        password --> 6-20 characters, use form to limit min_length
        gender --> 1 character, only Male and Female can be choosed
        register_datetime --> when create a user account, assign it with datetime.datetime.now
        last_login_datetime --> when create a user account, assign it with datetime.datetime.now
        role_type --> for now, only Student and Teacher can be choosed
        privacy_setting --> 1 character ALL, Same School and Friend only
        visited_count --> when create a user account, assign it with 0

    Optional Fields are:
        birthday --> DateField
        image --> if blank, use a default image
        domainname_suffix --> limit 30 characters
        friends --> a user can have no friends
        friend_applications_to --> a user can have no applications to him/her
        people_visited_to --> a user can have no people visit to him/her
        description -->to describe a person by himself
        
    """

    email = models.EmailField(unique = True)
    name = models.CharField(max_length = 10)
    password = models.CharField(max_length = 20)
    gender = models.CharField(max_length = 1, choices = GENDER_CHOICES)
    register_datetime = models.DateTimeField(default = datetime.datetime.now)
    last_login_datetime = models.DateTimeField(default = datetime.datetime.now)
    role_type = models.CharField(max_length = 1, choices = ROLE_CHOICES)
    privacy_setting = models.CharField(max_length = 1, choices = PRIVACY_CHOICES)
    visited_count = models.IntegerField(default = 0)

    birthday = models.DateField(null = True, blank = True)
    image = models.ImageField(upload_to = 'user_image/%Y/%m/%d', max_length = 120, null = True, blank = True)
    domainname_suffix = models.CharField(max_length = 30, null = True, blank = True)
    friends = models.ManyToManyField('self', null = True, blank = True)
    friend_applications_to = models.ManyToManyField('self', symmetrical = False,
                                                    through = 'FriendApply', related_name = 'friend_applications_from',
                                                    null = True, blank = True)
    people_visited_to = models.ManyToManyField('self', symmetrical = False,
                                               through = 'PersonVisited', related_name = 'people_visited_from',
                                               null = True, blank = True)

    description = models.CharField(max_length = 100,null = True, blank = True)

    def __unicode__(self):
        return self.email + u'(' + self.name + u')'

class Student(models.Model):
    """Definition of Student

    Necessary Fields are:
        user --> OneToOneField
        department --> ForeignKey
        admission_year --> Auto Generated
        role_type --> 1 character Bachelor, Master or Doctor
    Optional Fields are:
        research_domain --> may be useful when recommends
    
    """

    user = models.OneToOneField('User')
    department = models.ForeignKey('Department')
    admission_year = models.IntegerField()
    role_type = models.CharField(max_length = 1, choices = DEGREE_CHOICES)
    
    research_domain = models.CharField(max_length = 20, null = True, blank = True)

    def __unicode__(self):
        return self.user.name

class Teacher(models.Model):
    """Definition of Teacher

    Necessary Fields are:
        user --> OneToOneField
        department --> ForeignKey
        role_type --> 1 character Professor, Associate Professor or Lecterer, Researcher, Associate Researcher
    Optional Fields are:
        research_domain --> may be useful when recommends
    
    """
    
    user = models.OneToOneField('User')
    department = models.ForeignKey('Department')
    role_type = models.CharField(max_length = 2, choices = POST_CHOICES)
    
    research_domain = models.CharField(max_length = 20, null = True, blank = True)
    
    def __unicode__(self):
        return self.user.name

class School(models.Model):
    """Definition of School

    Necessary Fields are:
        name --> limit 30 characters    

    """
    
    class Meta:
        ordering = ['id']
    
    name = models.CharField(max_length = 30, unique = True)

    def __unicode__(self):
        return self.name

class Department(models.Model):
    """Definition of Department

    Necessary Fields are:
       school --> ForeignKey
       name --> limit 30 characters
        
    """
    
    class Meta:
        unique_together = ('school', 'name')
        ordering = ['id']

    school = models.ForeignKey('School')
    name = models.CharField(max_length = 30)

    def __unicode__(self):
        return self.school.name + u'@' + self.name

class FriendApply(models.Model):
    """Definition of FriendApply, a through table

    Necessary Fields are:
        user_from --> ForeignKey
        user_to --> ForeignKey
        content --> limit 150 characters
        apply_datetime --> DateTimeField

    """

    class Meta:
        unique_together = ('user_from', 'user_to')
        ordering = ['-apply_datetime']
    
    user_from = models.ForeignKey('User', related_name = 'friendapply_from')
    user_to = models.ForeignKey('User', related_name = 'friendapply_to')
    content = models.CharField(max_length = 150)
    apply_datetime = models.DateTimeField(default = datetime.datetime.now)

    def __unicode__(self):
        return self.user_from.name + u'-->' + self.user_to.name

class PersonVisited(models.Model):
    """Definition of PersonVisited, a through table

    Necessary Fields are:
        user_from --> ForeignKey
        user_to --> ForeignKey
        visit_datetime --> DateTimeField

    """

    class Meta:
        unique_together = ('user_from', 'user_to')
        ordering = ['-visit_datetime']
    
    user_from = models.ForeignKey('User', related_name = 'personvisited_from')
    user_to = models.ForeignKey('User', related_name = 'personvisited_to')
    visit_datetime = models.DateTimeField(default = datetime.datetime.now)

    def __unicode__(self):
        return self.user_from.name + u'-->' + self.user_to.name


    
